﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.ServiceModel.DomainServices.Hosting;
using System.ServiceModel.DomainServices.Server;
using DomainDriver.DomainModeling.Repositories;
using AutomotivePricingModel.Companies;

namespace AutomotivePricingSilverlightApp.Web.Services
{
    [EnableClientAccess()]
    public class CompaniesDomainService : DomainService
    {
        private static List<Company> s_Companies;

        static CompaniesDomainService()
        {
            Company honda = new Company();
            honda.CompanyName = "Honda";
            Company toyota = new Company();
            toyota.CompanyName = "Toyota";

            IRepository<Guid, Company> companies = DmConfiguration.DomainModel.GetRepository<Guid, Company>();
            companies.Add(honda);
            companies.Add(toyota);
        }

        public IQueryable<Company> GetCompanies()
        {
            IRepository<Guid, Company> companiesRep = DmConfiguration.DomainModel.GetRepository<Guid, Company>();
            ICollection<Company> companies = companiesRep.Read();
            IQueryable<Company> queryable = companies.AsQueryable<Company>();
            return queryable;
        }

        public Company UpdateCompanyName(Guid key, string companyName)
        {
            IRepository<Guid, Company> companiesRep = DmConfiguration.DomainModel.GetRepository<Guid, Company>();
            if (!companiesRep.Exists(key))
            { return null; }

            Company company = companiesRep.Read(key);
            company.CompanyName = companyName;
            companiesRep.Update(company);
            company = companiesRep.Read(key);
            return company;
        }

        public Company UpdateCeoName(Guid key, string ceoName)
        {
            IRepository<Guid, Company> companiesRep = DmConfiguration.DomainModel.GetRepository<Guid, Company>();
            if (!companiesRep.Exists(key))
            { return null; }

            Company company = companiesRep.Read(key);
            company.CeoName = ceoName;
            companiesRep.Update(company);
            company = companiesRep.Read(key);
            return company;
        }
    }
}